<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">鼠标的位置为：({{ x }}, {{ y }})</div>
  </body>
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  <script>
    const { createApp, ref, onMounted, onUnmounted } = Vue;

    const app = createApp({
      setup() {
        const x = ref(0);
        const y = ref(0);

        function updatePosition(event) {
          x.value = event.pageX;
          y.value = event.pageY;
        }

        onMounted(() => {
          window.addEventListener("mousemove", updatePosition, false);
        });
        onUnmounted(() => {
          window.removeEventListener("mousemove", updatePosition, false);
        });
        return {
          x,
          y,
        };
      },
    });

    app.mount("#app");
  </script>
</html>
